package com.kfm.am.tcpMany;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {

    public static void main(String[] args) throws IOException {
        // 服务端
        System.out.println("服务端启动成功...");
        ServerSocket serverSocket = new ServerSocket(8888);
        Socket socket = serverSocket.accept(); // 连接对象
        InputStream inputStream = socket.getInputStream();
        DataInputStream in = new DataInputStream(inputStream);

        OutputStream out = socket.getOutputStream();  // 获取连接的输出流，写东西到服务端、
        DataOutputStream dataOut = new DataOutputStream(out);
        Scanner sc = new Scanner(System.in);
        while (true) {
            try {
                String message = in.readUTF();
                System.out.println(message);
                System.out.println("【系统】你想回复什么？");
                String receive = sc.next();
                dataOut.writeUTF(receive);
                dataOut.flush();
            } catch (IOException e) {
                inputStream.close();
                socket.close();
                serverSocket.close();
                break;
            }
        }

    }
}
